$(function(){
	
	/*
	 * 初始化时间
	 */
	$('.time').datetimepicker({
		dayViewHeaderFormat: 'YYYY MMMM',
		locale: 'zh-cn',
		format: 'YYYY-MM-DD',
		keepInvalid: true
	});	 

	$('#beginTime,#endTime').data('DateTimePicker').useCurrent(false);
	$("#beginTime").on("dp.change", function (e) {
		$('#endTime').data("DateTimePicker").minDate(e.date);
	});
	$("#endTime").on("dp.change", function (e) {
		$('#beginTime').data("DateTimePicker").maxDate(e.date);
	});
	
	
	var noticeId = null;

	function NoticeModal(modalEle) {
		var self = this;
		self.$ele = modalEle;
		$('.save-btn',self.$ele).on('click',function(){
			self._onSave();
		});	
		$('.cancel-btn',self.$ele).on('click',function() {
			self._onCancel();
		});	
	//	self.tagBox = new TagBox($('.tag-box',self.$ele));
		self._initModal();
		self._initValidate();
	}

	NoticeModal.prototype = {
		constructor: NoticeModal,
		$ele: null,		
		$permissionBox: null,
		$selectBox: null,
		$validater: null,
		tagBox: null,
		_initModal: function () {
			$(this.$ele).modal({
				backdrop: 'static',
				keyboard: false,	
				show: false
			});
		},
		_initValidate: function() {
			this.validater = $('#notice-create-form').validate({
				rules: {
					"title": "required",
					"beginTime": "required",
					"endTime": "required",
					"totalNum": {
						required: true,
						number: true,
						min:1
					},
					"linkUrl": {
						required: true,
						url: true,
						maxlength:300
					},
				},
				messages:{
					"title": {
						required: "请输入标题"
					},
					"totalNum": {
						required: "请输入规模人数",
						number: "请输入合法的数字",
						min:"请输入合法的数字"
					},
				},
				showErrors: function(errorMap, errorList){
								if(errorList.length > 0) {
									var error = errorList[0];
									$(error.element).parent().addClass('has-error');
									$('#notice-create-msg').show().find('p').text(error.message);
								}
							},
				onkeyup: function(ele) {
							$('#notice-create-msg').hide();
							$(ele).parent().removeClass('has-error');
						 },
				success: function(label,ele) {
							 console.log(lable);
							 console.log(ele);
						 },
				submitHandler: function(){
							//$('#user-create-box .overlay').show();
							saveNotice();
						   }
				
			});
	   },
		_onCancel: function() {
			$(this.$ele).modal('hide');
		},
		_onSave: function() {
			$('form',this.$ele).submit();
		},
		_reset: function () {
		//	this.tagBox.reset();
			$('#notice-create-modal input:text').each(function(){
				$(this).val('');
			});
			$('#notice-create-msg').hide();
			$('.has-error').removeClass('has-error');
		//	$('#user-create-box .overlay').hide();
		},
		close: function () {
			$(this.$ele).modal('hide');
		},
		open: function (pageItem) {
			this._reset();
		//	this.tagBox.setChecks(roleIdArr);
			if(pageItem) {
				$('.notice-title',this.$ele).html('编辑活动公告');
				for(var key in pageItem) {
					$('input[name="'+key+'"]',this.$ele).val(pageItem[key]);
				}
				var jsonStr = pageItem.extendAttrs;
				console.log(jsonStr);
				var attrs = JSON.parse(jsonStr);
				for(var key in attrs) {
					$('input[name="'+key+'"]',this.$ele).val(attrs[key]);
				}
			} else {
				$('.notice-title',this.$ele).html('新建活动公告');
			}
			$(this.$ele).modal('show');
		}
	};

	var noticeCreater = new NoticeModal($('#notice-create-modal'));

	$('#notice-create-btn').on('click',function(){
		noticeId = null;
		noticeCreater.open();
	});

	$('.edit-btn').on('click',function(){
		var $tr = $(this).parents('tr');
		noticeId = $tr.attr('data-noticeId');
		cAjax({
			url: '/web/category/getItem',
			type: 'get',
			data: {
				itemId: noticeId 
			},
			success: function(obj) {
				noticeCreater.open(obj.data);
			},
			error: function(obj) {
				console.log(obj);
		    }
		});
	});

	function saveNotice() {
		var data = {},attrs={},
			url = '';
		data['categoryId'] = $(".select2").val();
	    data['title'] = $.trim($('#title').val());
	    data['linkUrl'] = $.trim($('#linkUrl').val());
	    data['sort'] = $.trim($('#sort').val());
	    attrs['beginTime'] = $('#beginTime').val();
	    attrs['endTime'] = $('#endTime').val();
	    attrs['place'] = $('#place').val();
	    attrs['totalNum'] = $('#totalNum').val();
	    attrs['keyword'] = $('#keyword').val();
	    data['extendAttrs'] = JSON.stringify(attrs);
//		$('#model-create-modal input:text').each(function(){
//			var $this = $(this);
//			data[$this.attr('name')] = $.trim($this.val());
//		});
		if(noticeId == null) {
			url = '/web/category/addItem';
		} else {
			data['id'] = noticeId;
			url = '/web/category/editItem';
		}
	//	console.log(data);
		noticeCreater.close();

		cAjax({
			url: url,
			data: data,
			success: function(obj) {
				console.log(obj);
				greeting({
					msg: "公告创建成功",
					confirm: function(){
						window.location.reload();
					}

				});
			},
			error: function(obj){
				console.log(obj);
		   }
		});
	}

	$('.notice-del-btn').on('click',function(){
		var $this = $(this);
		actionConfirm({
			msg: "确定要删除吗？",
			confirm: function() {
				cAjax({
					url:'/web/category/deleteItem',
					type:'post',
					data:{itemId: $this.parents('tr').attr('data-noticeId')},
					success:function(obj){
						
						$this.parents('tr').addClass('deleted');
						$this.parent().html('<span class="label label-warning">已删除</span>');
						$('.deleted').remove();
					},
					error: function(obj) {
					}
				});
			}
		});
	});
});

